package musicstore.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import musicstore.biz.AlbumBiz;
import musicstore.biz.GenreBiz;
import musicstore.entity.Album;
import musicstore.entity.Genre;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class AlbumAction extends ActionSupport {

	private List<Album> albumList;

	public List<Album> getAlbumList() {
		return albumList;
	}

	private AlbumBiz albumBiz;

	public void setAlbumBiz(AlbumBiz albumBiz) {
		this.albumBiz = albumBiz;
	}

	public String list() {
		this.albumList = albumBiz.getAlbumsByDateReleased();
		return SUCCESS;
	}

	private int genreId;

	public void setGenreId(int genreId) {
		this.genreId = genreId;
	}

	public int getGenreId() {
		return genreId;
	}

	private int pageNum;
	private int totalPage;
	private int pageSize;

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public String getListByGenreId() {
		pageSize = 4;
		System.out.println(title);
		int rows = albumBiz.getListByGenreIdAndTitle(title, genreId, 0, 0)
				.size();
		totalPage = (int) Math.ceil((double) (rows / pageSize)) + 1;
		if (pageNum <= 1)
			pageNum = 1;
		albumList = albumBiz.getListByGenreIdAndTitle(title, genreId, pageNum,
				pageSize);
		return SUCCESS;
	}

	private int id;

	public void setId(int id) {
		this.id = id;
	}

	private Album album;

	public Album getAlbum() {
		return album;
	}

	public void setAlbum(Album album) {
		this.album = album;
	}

	public String detail() {
		album = albumBiz.getAlbumById(id);
		return SUCCESS;
	}

	private String title;
	private List<Genre> genreList;

	public List<Genre> getGenreList() {
		return genreList;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	private GenreBiz genreBiz;

	public void setGenreBiz(GenreBiz genreBiz) {
		this.genreBiz = genreBiz;
	}

	public String goodsBiz() {
		genreList = genreBiz.getAll();
		pageSize = 5;
		int rows = albumBiz.getListByGenreIdAndTitle(title, genreId, 0, 0)
				.size();
		totalPage = (int) Math.ceil((double) (rows / pageSize)) + 1;
		if (pageNum <= 1)
			pageNum = 1;
		albumList = albumBiz.getListByGenreIdAndTitle(title, genreId, pageNum,
				pageSize);
		return SUCCESS;
	}

	public String addPage() {
		genreList = genreBiz.getAll();
		if (id > 0)
			album = albumBiz.getAlbumById(id);
		return SUCCESS;
	}

	public String editAlbum() {
		if (album.getId() > 0)
			albumBiz.update(album);
		else
			albumBiz.add(album);
		return SUCCESS;
	}

	public String tips() throws IOException {
		HttpServletResponse resp = ServletActionContext.getResponse();
		List<String> list = albumBiz.getTitleByPrefix(title, 2);
		resp.setCharacterEncoding("UTF-8");
		Gson gson = new Gson();
		String line = gson.toJson(list);
		resp.getWriter().print(line);
		return null;
	}
}
